package com.group02.community.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group02.community.annotation.Log;
import com.group02.community.enmus.BusinessType;
import com.group02.community.entity.vo.VisitorVO;
import com.group02.community.service.IVisitorService;
import com.group02.community.utils.ResponseResult;
import com.group02.community.utils.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Api
@RestController
public class VisitorController {
    @Autowired
    private IVisitorService visitorService;

    @ApiOperation("查询所有的访客")
    @GetMapping("/visitors")
    public List<VisitorVO> getAllVisitors(){
        return visitorService.selectAllVisitors();
    }

    @ApiOperation("查询分页")
    @GetMapping("/visitors/page")
    public ResponseEntity<IPage<VisitorVO>> getVisitorPage(Long current, Long size, VisitorVO visitorVO){
        return ResponseEntity.ok(visitorService.getVisitorPage(current,size,visitorVO));
    }

    @ApiOperation("添加")
    @PostMapping("/visitor")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    public ResponseResult<String> addVisitor(VisitorVO visitorVO){
        visitorVO.setVisitorId(UUIDUtils.getId());
        visitorService.addVisitor(visitorVO);
        return ResponseResult.ok("添加成功");
    }

    @ApiOperation("更新")
    @PutMapping("/visitor")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    public ResponseResult<String> updateVisitor(@RequestBody VisitorVO visitorVO){
        visitorService.updateById(visitorVO);
        return ResponseResult.ok("更新成功");
    }

    @ApiOperation("删除")
    @DeleteMapping("/visitor/{visitorId}")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    public ResponseResult<String> deleteVisitor(@PathVariable long visitorId){
        visitorService.removeById(visitorId);
        return ResponseResult.ok("删除成功");
    }

    /**
     * 导出Excel
     * @param response
     * @return
     * @throws IOException
     */
    @ApiOperation("导出Excel")
    @GetMapping("/visitor/download")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    public ResponseEntity<String> downloadExcel(HttpServletResponse response) throws IOException {
        visitorService.downloadExcel(response);
        return ResponseEntity.ok("下载成功");
    }
}
